package lib;

public class CartesianPoint implements Point {

	public CartesianPoint(double aX, double aY) {
		x = aX;
		y = aY;
	}

	public double getX() {
		return x;
	}

	public double getY() {
		return y;
	}
	
	@Override
	public boolean isWithinBounds(GeometricArea area) {
		return area.contains(this, true);
	}

	@Override
	public int getDimensions() {
		return 2;
	}

	@Override
	public double getValue(int dim) {
		switch(dim) {
		case 0:
			return x;
		case 1:
			return y;
		default:
			throw new IllegalArgumentException("Invalid Dimension: " + dim);
		}
	}

	public String toString() {
		String output;
		output = String.format("(%f,%f)", x, y);
		return output;
	}
	
	public static CartesianPoint midpoint(CartesianPoint ptA, CartesianPoint ptB) {
		double x, y;
		CartesianPoint midpoint;
		x = (ptA.x + ptB.x)/2;
		y = (ptA.y + ptB.y)/2;
		midpoint = new CartesianPoint(x, y);
		return midpoint;
	}

	public static double distance(CartesianPoint ptA, CartesianPoint ptB) {
		double d;
		double dx, dy;
		dx = (ptA.x - ptB.x);
		dy = (ptA.y - ptB.y);
		d = Math.sqrt((dx * dx) + (dy * dy));
		return d;
	}

	private double x;
	private double y;
}
